
#include "vkboard.hpp"

#include <iostream>

extern "C" {
	
#include <sys/stat.h>
#include <fcntl.h>	
	
}

//////////////
// KeyEvent //
//////////////

std::map<char, short> KeyEvent::kmap;

KeyEvent::KeyEvent(const char *dev)
{
	if((fd = open(dev, O_WRONLY)) < 0) {
		std::cerr << "Unable to open for writing: " << dev << std::endl;
		return;
	}
	
	event.type = EV_KEY;
	event.value = 0;

	// todo: special characters for CAPSLOCK, ... ascii table
	
	kmap['Q'] = KEY_Q;
	kmap['W'] = KEY_W;
}

KeyEvent::~KeyEvent()
{
	close(fd);
}

void KeyEvent::keyEvent(char key)
{
	int w = sizeof(struct input_event);

	event.code = kmap[key];

	if(write(fd, &event, w) < w) {
		std::cerr << "Failed to write event" << std::endl;
	}

}
